
#include <BDriver>
#include <BViewer>
#include <BThread>
#include <BSurface>
#include <BSphere>
#include <BCamera>
#include <BViewer>
#include <BScene>
#include <BProgram>
#include <BMaterial>
#include <BState>
#include <BHBoxLayout>
#include <BApplication>
#include <BSphereControl>
#include <BMainWindow>
#include <BDialog>

using namespace BWE;

const char* shaderVertSource =
{
    "// microshader - colors a fragment based on its position\n"
    "varying vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "    color = gl_Vertex;\n"
    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    "}\n"
};

const char* shaderFragSource =
{
    "varying vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "    gl_FragColor = clamp( color, 0.0, 1.0 );\n"
    "}\n"
};

BProgram* createProgram()
{
	BProgram* program = new BProgram();

	BShader* shaderVert = new BShader(BShader::Type_Vertex);
	shaderVert->setText(shaderVertSource);
	program->addShader(shaderVert);

	BShader* shaderFrag = new BShader(BShader::Type_Fragment);
	shaderFrag->setText(shaderFragSource);
	program->addShader(shaderFrag);

	return program;
}

BGeometry* createRectangle(BProgram* program)
{
	BSurface* ground = new BSurface(30, 40, 10, 10);

	BMaterial* mate = new BMaterial;
	mate->setAmbient(BColor(128, 136, 112, 128));
	mate->setDiffuse(BColor(128, 136, 112, 128));
	ground->setMaterial(mate);
	ground->setProgram(program);

	return ground;
}

BGeometry* createSphere(BProgram* program)
{
	BSphere* sphere = new BSphere(5, 20, 20);
	sphere->setProgram(program);

	BMaterial* mate = new BMaterial;
	mate->setAmbient(BColor(66, 66, 66, 128));
	mate->setDiffuse(BColor(96, 136, 136, 128));
	mate->setSpecular(BColor(96, 136, 136, 128));
	mate->setShininess(0.8f);
	sphere->setMaterial(mate);

	return sphere;
}

int main(int argc, char** argv)
{
	BApplication app;

	BProgramHolder program = createProgram();

	BSceneHolder scene = new BScene();

	BNode* node = new BNode();
	{
		BGeometry* rectangle = createRectangle(program.ptr());
		BGeometry* sphere = createSphere(program.ptr());
		node->addShape(rectangle);
		node->addShape(sphere);
	}
	scene->addNode(node);

	BCameraHolder camera = new BCamera();
	camera->setPosition(30, 30, 30);
	camera->setCenter(0, 0, 0);

	BViewer* viewer = new BViewer();
	viewer->setScene(scene);
	viewer->setCamera(camera.get());
	viewer->setControl(new BSphereControl);
	viewer->setRect(50, 50, 800, 520);
	viewer->setBorder(10);
	viewer->setPerchs(5, 5, 5, 5);
	viewer->setMargins(5, 5, 5, 5);
	viewer->show();

	//BHBoxLayout* hlayout = new BHBoxLayout();
	//hlayout->addWidget(viewer);
	//
	//BDialog dialog;
	//dialog.setRect(200, 200, 500, 400);
	//dialog.setMargin(2, 2, 2, 2);
	//dialog.setLayout(hlayout);
	//dialog.show();

	return app.execute();
}
